<template>
  <div class="home">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="auto">
        <div class="logo"></div>
        <el-menu
        :unique-opened='true'
         default-active="2"
         class="el-menu-admin"
         :collapse="isCollapse"
         background-color="#F9F9F9"
         active-text-color="#409EFF"
         :router="true"
         @open="handleOpen"
         @close="handleClose"
        >
          <el-submenu :index="item.authName" v-for='(item,index) in menuList' :key='index'>
            <template slot="title"><i class="el-icon-location"></i>
            <span>{{item.authName}}</span>
            </template>
            <!-- :router='true' 添加路由 -->
            <el-menu-item :index="subItem.path" v-for='(subItem,index) in item.children' :key="index">
              <i class="el-icon-menu"></i>
              <span slot="title">{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边头部及主体部分 -->
      <el-container>
        <el-header>
          <i class="myicon myicon-menu toggle-btn" @click="toggleCollapse"></i>
          <div class="system-title">电商后台管理系统</div>
          <div>
            <span class="welcome">您好，{{$store.getters.getterName}}
              <!-- {{$store.getters.getterName}} -->
              </span>
            <el-button type="warning" round @click="logout">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
  // 引入获取用户数据列表的函数
  import { getUserList, getMenus } from "@/api";
  export default {
    // computed:{
    //   myname(){
    //     if(this.$store.state===''){
    //       this.$store.commit('a')
    //     }
    //     return this.$store.state.username
    //   }
    // },
    data() {
      return { isCollapse: false, menuList: [] };
    },
    created() {
      getUserList({ query: "", pagenum: 1, pagesize: 10 }),
        //  获取左侧菜单
        getMenus().then(res=>{
          if(res.meta.status===200){
            this.menuList=res.data
            // console.log(this.menuList);
          }
        })
    },
    methods: {
      handleOpen(key, keyPath) {
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
      },
      // 切换左侧菜单的展开与关闭
      toggleCollapse() {
        this.isCollapse = !this.isCollapse;
      },
      // 退出登录
      logout() {
        // 清空localStorage
        // 跳转到登陆页
        localStorage.removeItem("adminToken");
        this.$router.push({ name: "Login" });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .home {
    height: 100%;
  }
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #f9f9f9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409eff;
  }
  .logo {
    height: 60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292cf;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome {
    color: white;
  }
</style>
